package MULE.Model.Panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import MULE.Model.Inventory;
import MULE.Model.Resource;
import MULE.Model.Logic.StoreLogic;
import MULE.Model.Logic.StoreLogic.ExchangeType;

import java.awt.Font;
import java.util.EnumSet;

import javax.swing.UIManager;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;

/**
 * This class creates the JPanel that will display the Store object.
 * 
 * 
 * @ author Aaron Englebardt @ version 1.0 11.11.2013
 * 
 */

public class StorePanel extends JPanel {

	private JLabel resource, action, amount, mule, food, energy, smithore,
			crystite, welcome;
	private JComboBox muleBox, foodBox, energyBox, smithoreBox, crystiteBox;
	private JButton exitStore, completeTransaction;
	private JTextField muleField, foodField, energyField, smithoreField,
			crystiteField;
	private JLabel foodAvailable, energyAvailable, smithoreAvailable,
			crystiteAvailable, mulesAvailable;

	public StorePanel(Dimension panelSize) {
		setForeground(new Color(0, 0, 0));
		// init
		setLayout(null);

		this.setPreferredSize(new Dimension(590, 379));
		this.setBackground(UIManager.getColor("CheckBox.background"));
		// sets location of each object displayed by the panel
		resource = new JLabel("Resource");
		resource.setOpaque(true);
		resource.setBackground(Color.LIGHT_GRAY);
		resource.setForeground(new Color(0, 0, 0));
		resource.setFont(new Font("Dialog", Font.BOLD, 14));
		resource.setBounds(20, 50, 100, 20);
		this.add(resource);

		action = new JLabel("Action");
		action.setBackground(Color.LIGHT_GRAY);
		action.setOpaque(true);
		action.setForeground(new Color(0, 0, 0));
		action.setFont(new Font("Dialog", Font.BOLD, 14));
		action.setBounds(175, 50, 100, 20);
		this.add(action);

		amount = new JLabel("Amount");
		amount.setBackground(Color.LIGHT_GRAY);
		amount.setOpaque(true);
		amount.setForeground(new Color(0, 0, 0));
		amount.setFont(new Font("Dialog", Font.BOLD, 14));
		amount.setBounds(370, 50, 100, 20);
		this.add(amount);

		food = new JLabel("Food");
		food.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
		food.setForeground(new Color(0, 0, 0));
		food.setBounds(30, 88, 100, 20);
		this.add(food);

		energy = new JLabel("Energy");
		energy.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
		energy.setForeground(new Color(0, 0, 0));
		energy.setBounds(30, 128, 100, 20);
		this.add(energy);

		smithore = new JLabel("Smithore");
		smithore.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
		smithore.setForeground(new Color(0, 0, 0));
		smithore.setBounds(30, 168, 100, 20);
		this.add(smithore);

		crystite = new JLabel("Crystite");
		crystite.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
		crystite.setForeground(new Color(0, 0, 0));
		crystite.setBounds(30, 208, 100, 20);
		this.add(crystite);

		mule = new JLabel("Mule");
		mule.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
		mule.setForeground(new Color(0, 0, 0));
		mule.setBounds(30, 248, 100, 20);
		this.add(mule);

		// Establishes the location and content of the JComboBoxes.
		String boxString[] = { "BUY", "SELL" };

		muleBox = new JComboBox(boxString);
		muleBox.setBounds(175, 248, 100, 20);
		// muleBox.addActionListener(new ExchangeTypeListener(muleExchange));
		this.add(muleBox);

		foodBox = new JComboBox(boxString);
		foodBox.setBounds(175, 88, 100, 20);
		// foodBox.addActionListener(new ExchangeTypeListener(foodExchange));
		this.add(foodBox);

		energyBox = new JComboBox(boxString);
		energyBox.setBounds(175, 128, 100, 20);
		// energyBox.addActionListener(new
		// ExchangeTypeListener(energyExchange));
		this.add(energyBox);

		smithoreBox = new JComboBox(boxString);
		smithoreBox.setBounds(175, 168, 100, 20);
		// smithoreBox.addActionListener(new
		// ExchangeTypeListener(smithoreExchange));
		this.add(smithoreBox);

		crystiteBox = new JComboBox(boxString);
		crystiteBox.setBounds(175, 208, 100, 20);
		// crystiteBox.addActionListener(new
		// ExchangeTypeListener(crystiteExchange));
		this.add(crystiteBox);

		completeTransaction = new JButton("Complete Transaction");
		completeTransaction.setBounds(353, 280, 200, 30);
		completeTransaction.setActionCommand(StoreLogic.ActionCommand.EXCHANGE
				.toString());
		this.add(completeTransaction);

		// foodField, energyField, smithoreField, crystiteField;

		foodField = new JTextField(5);
		foodField.setBounds(370, 88, 100, 20);
		this.add(foodField);

		energyField = new JTextField(5);
		energyField.setBounds(370, 128, 100, 20);
		this.add(energyField);

		smithoreField = new JTextField(5);
		smithoreField.setBounds(370, 168, 100, 20);
		this.add(smithoreField);

		crystiteField = new JTextField(5);
		crystiteField.setBounds(370, 208, 100, 20);
		this.add(crystiteField);

		muleField = new JTextField(5);
		muleField.setBounds(370, 248, 100, 20);
		this.add(muleField);

		foodAvailable = new JLabel("(available)");
		foodAvailable.setForeground(new Color(0, 0, 0));
		foodAvailable.setBounds(477, 91, 107, 16);
		add(foodAvailable);

		energyAvailable = new JLabel("(available)");
		energyAvailable.setForeground(new Color(0, 0, 0));
		energyAvailable.setBounds(477, 131, 107, 16);
		add(energyAvailable);

		smithoreAvailable = new JLabel("(available)");
		smithoreAvailable.setForeground(new Color(0, 0, 0));
		smithoreAvailable.setBounds(477, 171, 107, 16);
		add(smithoreAvailable);

		crystiteAvailable = new JLabel("(available)");
		crystiteAvailable.setForeground(new Color(0, 0, 0));
		crystiteAvailable.setBounds(477, 211, 107, 16);
		add(crystiteAvailable);

		mulesAvailable = new JLabel("(available)");
		mulesAvailable.setForeground(new Color(0, 0, 0));
		mulesAvailable.setBounds(477, 251, 107, 16);
		add(mulesAvailable);

		JPanel headerPanel = new JPanel();
		headerPanel.setBackground(Color.DARK_GRAY);
		headerPanel.setBorder(new MatteBorder(0, 0, 1, 0, (Color) Color.BLACK));
		headerPanel.setBounds(0, 0, 584, 42);
		add(headerPanel);
		headerPanel.setLayout(null);

		exitStore = new JButton("<-- Back to Town");
		exitStore.setBounds(16, 7, 158, 29);
		headerPanel.add(exitStore);
		exitStore.setActionCommand(StoreLogic.ActionCommand.EXIT_STORE
				.toString());

		welcome = new JLabel("WELCOME TO THE STORE");
		welcome.setBounds(283, 11, 276, 25);
		headerPanel.add(welcome);
		welcome.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
		welcome.setHorizontalAlignment(SwingConstants.CENTER);
		welcome.setForeground(Color.WHITE);
	}

	/**
	 * Adds the given action listener to the panel's exit button.
	 * 
	 * @param listener
	 *            The exit button's new listener.
	 */
	public void addExitButtonListener(ActionListener listener) {
		exitStore.addActionListener(listener);
	}

	public void addTransactionButtonListener(ActionListener listener) {
		completeTransaction.addActionListener(listener);
	}

	public void updateAvailableResources(Inventory available) {
		for (Resource resource : EnumSet.allOf(Resource.class)) {
			JLabel availLabel = getAvailLabel(resource);
			int amnt = available.getAmount(resource);
			availLabel.setText("(" + amnt + " available)");
		}
	}

	private JLabel getAvailLabel(Resource resource) {
		JLabel label;
		if (resource == Resource.FOOD) {
			label = foodAvailable;
		} else if (resource == Resource.ENERGY) {
			label = energyAvailable;
		} else if (resource == Resource.SMITHORE) {
			label = smithoreAvailable;
		} else if (resource == Resource.CRYSTITE) {
			label = crystiteAvailable;
		} else {
			label = mulesAvailable;
		}
		return label;
	}

	public JComboBox getBoxForResource(Resource resource) {
		JComboBox selectedBox;
		if (resource == Resource.FOOD) {
			selectedBox = foodBox;
		} else if (resource == Resource.ENERGY) {
			selectedBox = energyBox;
		} else if (resource == Resource.SMITHORE) {
			selectedBox = smithoreBox;
		} else if (resource == Resource.CRYSTITE) {
			selectedBox = crystiteBox;
		} else {
			selectedBox = muleBox;
		}
		return selectedBox;
	}

	public JTextField getFieldForResource(Resource resource) {
		JTextField selectedField;
		if (resource == Resource.FOOD) {
			selectedField = foodField;
		} else if (resource == Resource.ENERGY) {
			selectedField = energyField;
		} else if (resource == Resource.SMITHORE) {
			selectedField = smithoreField;
		} else if (resource == Resource.CRYSTITE) {
			selectedField = crystiteField;
		} else {
			selectedField = muleField;
		}
		return selectedField;
	}

	public ExchangeType getExchangeType(Resource resource) {
		JComboBox selectedBox = getBoxForResource(resource);
		return (selectedBox.getSelectedIndex() == 0) ? ExchangeType.BUY
				: ExchangeType.SELL;
	}

	public int getAmount(Resource resource) {
		JTextField selectedField = getFieldForResource(resource);
		String input = selectedField.getText();
		int amount = (input.equals("")) ? 0 : Integer.parseInt(input);
		return amount;
	}

	public void resetInterface() {
		JTextField fields[] = { foodField, energyField, smithoreField,
				crystiteField, muleField };
		for (JTextField field : fields) {
			field.setText("");
		}
	}
}
